'use strict';

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    baseController = require('../../../Base/server/controllers/BaseDoc.js');

//条码主档
var newModel = baseController.getBaseDocFields();
//条码
newModel.Barcode = {
    type: String,
    trim: true
};

//类型 0: 物料 1: 部件 2: 仓库 3: 容器 4: 设备 5: 人 6: 其他
newModel.BarcodeType = {
    type: Number
};

//当前状态 0: 未激活、1:已激活、2:已下线、3:已报废、4:已完工、5:芯片失效
newModel.Status = {
    type: Number
};

//实体ID
newModel.EntityID = {
    type: String,
    trim: true
};

//装配标识
newModel.IsAssembly = {
    type: Boolean
};
//装配关系
newModel.AssemblyRelation = {
    type: Schema.ObjectId,
    ref: 'AssemblyRelation'
};

//外部条码
newModel.OutsideBarcode = {
    type: String,
    trim: true
};

//当前流水
newModel.BizRecord = {
    type: Schema.ObjectId,
    ref: 'BizRecord'
};

//当前工序
newModel.Operation = {
    type: Schema.ObjectId,
    ref: 'Operation'
};
//工序名称
newModel.OperationName = {
    type: String,
    trim: true
};

//当前作业
newModel.Work = {
    type: Schema.ObjectId,
    ref: 'Work'
};
//作业名称
newModel.WorkName = {
    type: String,
    trim: true
};

//当前工位
newModel.WorkLocation = {
    type: Schema.ObjectId,
    ref: 'WorkLocation'
};
//工位名称
newModel.WorkLocationName = {
    type: String,
    trim: true
};

//当前终端
newModel.Terminal = {
    type: Schema.ObjectId,
    ref: 'Terminal'
};
//终端名称
newModel.TerminalName = {
    type: String,
    trim: true
};

//当前用户集合
newModel.Users = {
    type: [
        {
            type: Schema.ObjectId,
            ref: 'User'
        }
    ]
};

//当前质检类型 0: 全检 1:巡检 2:首件质检
newModel.QCType = {
    type: Number
};
//当前质检状态 0: 合格 1:不合格 2:让步接收 3:品质判定 4:返工 5:报废 6：特裁
newModel.QCStatus = {
    type: Number
};
//当前质检返工工序
newModel.QCReworkOperation = {
    type: Schema.ObjectId,
    ref: 'Operation'
};
//返工次数
newModel.ReworkQty = {
    type: Number
};
//生产批号 
newModel.MOLotNumber = {
    type: String,
    trim: true
};
//出货批号 出货批次
newModel.ShipLotNumber = {
    type: String,
    trim: true
};
//生产订单
newModel.MO = {
    type: Schema.ObjectId,
    ref: 'MO'
};
//工单编码
newModel.MOCode = {
    type: String,
    trim: true
};
//物料
newModel.ItemMaster = {
    type: Schema.ObjectId,
    ref: 'ItemMaster'
};
//物料编码
newModel.ItemMasterCode = {
    type: String,
    trim: true
};
//物料名称
newModel.ItemMasterName = {
    type: String,
    trim: true
};
//物料规格
newModel.Specification = {
    type: String,
    default: '',
    trim: true
};

//完工计划
newModel.CompletePlan = {
    type: Schema.ObjectId,
    ref: 'CompletePlan'
};

//已产生完工报告标识
newModel.IsERPComplete = {
    type: Boolean
};
//报废标识
newModel.IsScrap = {
    type: Boolean
};
//打印次数
newModel.PrintQty = {
    type: Number
};

//扩展字段:
newModel.DescSeg1 = {
    type: String,
    default: '',
    trim: true
};
newModel.DescSeg2 = {
    type: String,
    default: '',
    trim: true
};

var newSchema = new Schema(newModel);
newSchema.index({Barcode: 1}, {"unique": true});
newSchema.index({Barcode: 1, QCStatus: 1});

mongoose.model('BarcodeMaster', newSchema, 'BarcodeMaster');
